/**
 * Created by 鹏 on 14-3-12.
 */
var db = require('../common/db');
var userService = require('../Service/userService.js');

exports.memberInProject = function (projectId, callback) {
    db.execSql("select * from user inner join teammember on user.UserId = teammember.UserId where teammember.ProjectId=?", [projectId], function (err, data) {
        callback(data);
    });
};

exports.memberCandidate = function (projectId, callback) {
    db.execSql("select * from user where user.UserId not in (select UserId from teammember where ProjectId = ? )", [projectId], function (err, data) {
        callback(data);
    });
};

exports.addMember = function (projectId, email, callback) {
    userService.getUser(email, function (user) {
        var member = {
            ProjectId: projectId,
            UserId: user.UserId
        };
        db.execSql("INSERT INTO TeamMember SET ?", member, function (err, data) {
            if (callback != null) {
                callback();
            }
        });
    });
};

exports.removeMember = function (projectId, userId, callback) {
    db.execSql("delete from TeamMember where ProjectId = ? and UserId = ?", [projectId, userId], function (err, data) {
        if (callback != null) {
            callback();
        }
    });
};